#include "tool.h"	
#include "io.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

linedata *ReadFile2Lines(const char *filepath)
{
	char line[LINE]={0};
        char *readchars=NULL;
        char **strarr=NULL;
        char *ptmp=NULL;
        char lastchar=0;
	char buff[BUFF];
        linedata *pHead=NULL;
        linedata *ppre=NULL;
        linedata *plinedata=NULL;
        FILE *pfile=fopen(filepath,"r");


        if(pfile==NULL)
        {
                printf("open file fail");
                return 0;
        }



        while(fgets(buff,sizeof(BUFF-1),pfile)!=NULL)
        {

        //      printf("%s\n",buff);

                lastchar=buff[BUFF-2];
                strncat(line,buff,sizeof(buff));

                if(lastchar=='\n')
                {



                        trimchars(line);
                        ptmp=(char *)malloc(sizeof(char)*strlen(line));
                        strcpy(ptmp,line);
                        plinedata=(linedata *)malloc(sizeof(linedata));
                        plinedata->str=ptmp;
                        plinedata->pNext=NULL;
                        plinedata->hashcode=Myhashchars(ptmp);
			if(pHead==NULL)
                        {

                                ppre=pHead=plinedata;
                        }
                        else{

                                        ppre->pNext=plinedata;
                                        ppre=plinedata;
                        }

                //      printf("output chars%s\n",line);
                        line[0]=0;
                        lastchar=0;
                }

                memset(buff,0,BUFF);

        }

	return pHead;
}



/**
 *free the memory
 */
void FreeFileReaderData(linedata *pHead)
{

	linedata *pcurt=pHead;
	linedata *ppre=pcurt;



	while(pcurt!=NULL)
	{
		pcurt=pcurt->pNext;
		free(ppre);
		ppre=pcurt;		
	}


}
